//
//  HistorySearchBar.m
//  sdk2.0zhengquandasha
//
//  Created by 胡阳 on 15/12/20.
//  Copyright © 2015年 palmaplus. All rights reserved.
//

#import "HistorySearchBar.h"

@interface HistorySearchBar ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>
@property (nonatomic,strong)UITableView *tableview;
@property (nonatomic,strong)NSMutableArray *historyArr;
@end

@implementation HistorySearchBar

-(UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.scrollEnabled = YES;
        _tableview.backgroundColor = [UIColor whiteColor];
        _tableview.frame = CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 300);
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 30);
        [btn setTitle:@"清空搜索历史记录" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(resetHistory) forControlEvents:UIControlEventTouchUpInside];
        _tableview.tableFooterView = btn;
    }
    return _tableview;
}

-(NSMutableArray *)historyArr{
    if (!_historyArr) {
        _historyArr = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"searchhistory"]];
        if (!_historyArr) {
            _historyArr = [[NSMutableArray alloc]init];
        }
    }
    return _historyArr;
}

-(void)resetHistory{
    NSLog(@"清空搜索历史记录");
    [self.historyArr removeAllObjects];
    [self saveSearchHistory:@""];
}

-(instancetype)init{

    if (self = [super init]) {
        self.delegate = self;
    }
    return self;
}

-(void)willMoveToSuperview:(UIView *)newSuperview{
//    [newSuperview addSubview:self.tableview];
  
    [self.mainView addSubview:self.tableview];
      self.tableview.hidden = YES;
}
#pragma mark- tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.maxCount <= self.historyArr.count) {
        return self.maxCount;
    }
    return self.historyArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

//-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    return 30;
//}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *ID = @"id";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.textLabel.text = self.historyArr[indexPath.row];
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

           self.hisBlock((int)indexPath.row,self.historyArr[indexPath.row]);
}

#pragma mark- searchBarDelegate

-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    [self becomeFirstResponder];
    self.tableview.hidden = NO;
//    NSLog(@"%@",self.superview.superview);
//    [self.superview insertSubview:self.tableview atIndex:self.superview.subviews.count - 1];
}



-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [self saveSearchHistory:self.text];
}


-(void)saveSearchHistory:(NSString *)text{
    //NSMutableArray *arr = [[NSUserDefaults standardUserDefaults] objectForKey:@"searchhistory"];
    if ([self.historyArr containsObject:text]) {
        return;
    }
    if ([text isEqualToString:@""]) {
        
    }else{
    [self.historyArr insertObject:self.text atIndex:0];
    }
    if (self.historyArr.count > self.maxCount && self.maxCount != 0) {
        [self.historyArr removeObjectAtIndex:self.maxCount];
    }
    [self.tableview reloadData];
    [[NSUserDefaults standardUserDefaults] setObject:self.historyArr forKey:@"searchhistory"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
@end
